package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.feignclient.LoginFeignClient;
import cn.itsource.ymcc.service.IEmployeeService;
import cn.itsource.ymcc.domain.Employee;
import cn.itsource.ymcc.query.EmployeeQuery;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.result.PageList;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    public IEmployeeService employeeService;


    @Autowired
    private LoginFeignClient loginFeignClient;


    @GetMapping("/getEmployeeByLoginId/{loginId}")
    public JsonResult getEmployeeByLoginId(@PathVariable("loginId")Long loginId){
        return JsonResult.success().setData(employeeService.getEmployeeByLoginId(loginId));
    }
    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    @GlobalTransactional(rollbackFor = Exception.class)
    public JsonResult saveOrUpdate(@RequestBody @Valid Employee employee){
        if(employee.getId()!=null){
            employeeService.updateById(employee);
        }else{
            Login login = new Login();
            login.setUsername(employee.getTel());
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(Constants.DEFAULT_PWD);
            login.setPassword(encode);
            login.setType(RConst.Num.INT_0);
            login.setEnabled(RConst.Num.INT_1);

            JsonResult jsonResult = loginFeignClient.saveOrUpdate(login);
            Integer data = (Integer) jsonResult.getData();
            employee.setLoginId(data.longValue());
            employeeService.save(employee);

        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        employeeService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(employeeService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(employeeService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody EmployeeQuery query){
        Page<Employee> page = new Page<Employee>(query.getPageNo(), query.getPageSize());
        if (query.getKeyword()!=null && query.getKeyword().replaceAll(" ","")!=""){
            page = employeeService.page(page,new LambdaQueryWrapper<Employee>().like(Employee::getRealName,query.getKeyword()));
        }else {
            page = employeeService.page(page);
        }
        return JsonResult.success().setData(new PageList<Employee>(page.getTotal(), page.getRecords()));
    }
}
